package com.sssoft.Yundian.activity;

import java.util.HashMap;
import java.util.List;

import org.w3c.dom.Text;

import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.GoodsDao;
import com.sssoft.Yundian.Dao.PordersDao;
import com.sssoft.Yundian.bean.Goods;
import com.sssoft.Yundian.bean.Pgoods;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 收款页面Activity
 * 
 * @author Jiang 2017.10.11
 *
 */

public class ConsumptionActivity extends Activity {
	private List<Goods> goods;
	private HashMap<Integer, Integer> pgoods;
	private GoodsDao gdao;
	private PordersDao pordersdao;
	private ConsumAdapter cAdapter;

	private long orders_id;

	private ListView lv_show_cus;
	private TextView tv_money;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_consumption);

		initView();
	}

	public void initView() {

		lv_show_cus = (ListView) findViewById(R.id.lv_show_cus);
		tv_money = (TextView) findViewById(R.id.tv_money);

		Intent intent = getIntent();
		orders_id = intent.getLongExtra("orders_id", -1);
		if (orders_id != -1) {
			gdao = new GoodsDao(ConsumptionActivity.this);
			pordersdao = new PordersDao(ConsumptionActivity.this);
			pgoods = pordersdao.findGoodid(orders_id);
			goods = gdao.findMarkGood(orders_id, pgoods);
			cAdapter = new ConsumAdapter();
			lv_show_cus.setAdapter(cAdapter);
		} else {
			Toast.makeText(ConsumptionActivity.this, "������", Toast.LENGTH_SHORT).show();
		}

		tv_money.setText(totalmoney() + "");

	}

	public void toback(View v) {
		finish();
	}

	public float totalmoney() {
		float sum = 0;
		for (int i = 0; i < goods.size(); i++) {
			if (goods.get(i).isSelected()) {
				sum = sum + Float.parseFloat(goods.get(i).getGoods_price()) * goods.get(i).getSelectedNum();
			}
		}
		
		return sum;
	}

	class ConsumAdapter extends BaseAdapter {

		@Override
		public int getCount() {

			return goods.size();
		}

		@Override
		public Object getItem(int position) {
			return goods.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			View item = convertView != null ? convertView
					: View.inflate(getApplicationContext(), R.layout.show_cusumption, null);

			TextView tv_goodname = (TextView) item.findViewById(R.id.tv_goodname);
			TextView tv_goodprice = (TextView) item.findViewById(R.id.tv_goodprice);
			TextView tv_goodnum = (TextView) item.findViewById(R.id.tv_goodnum);
			TextView tv_goodsum = (TextView) item.findViewById(R.id.tv_goodsum);

			final Goods a = goods.get(position);

			tv_goodname.setText(a.getGoods_name() + "");
			tv_goodprice.setText(a.getGoods_price() + "");
			tv_goodnum.setText(a.getSelectedNum() + "");
			float total = Float.parseFloat(a.getGoods_price()) * a.getSelectedNum();
			tv_goodsum.setText(total + "");
			return item;
		}
	}
}
